package com.lin.mr.maizilaundry.myadapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lin.mr.maizilaundry.R;
import com.lin.mr.maizilaundry.activity.PayActivity;
import com.lin.mr.maizilaundry.activity.RegisterActivity;
import com.lin.mr.maizilaundry.entity.Laundry;
import com.lin.mr.maizilaundry.utils.CountEvent;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by lenovog on 2016/8/17.
 */
public class BasketAdapter extends BaseAdapter implements View.OnClickListener {
    private TextView previous_total;//优惠前的费用
    private TextView after_total;//优惠后的费用

    private ImageView iv_basket;//空篮子

    private ImageView iv_basket_jump;//跳转到用户信息页面
    private RelativeLayout rl_basket_jump;//跳转到用户信息页面
    private Button btn_pay;//支付按钮

    private ImageView iv_piture;//衣服图片
    private TextView tv_name;//衣服名称
    private TextView tv_num;//件数
    private TextView tv_value;//价格
    private ImageView iv_minus;
    private ImageView iv_plus;
    private ImageView iv_cancle;//取消该项

    private final int ITEM1 = 0;
    private final int ITEM2 = 1;
    private final int ITEMSUM = 2;
    private int currentType;

    int clothCount;//ListView中的每一项的衣服的件数
    private Laundry laundry;
    private List<Laundry> list;
    private View view_coupon;
    private Context context;
    private EventBus eventBus;

    //构造函数

    /**
     *
     * @param context
     * @param list
     * @param view_coupon:优惠券和立即支付按钮的布局
     * @param iv_basket:空篮子的背景图片
     */
    public BasketAdapter(Context context, List<Laundry> list,
                         View view_coupon, ImageView iv_basket) {
        this.context = context;
        this.list = list;
        this.view_coupon = view_coupon;
        this.iv_basket = iv_basket;
    }

    @Override
    public int getCount() {
        //根据list的容量判断
        if (list.size() == 0) {
            iv_basket.setVisibility(View.VISIBLE);
            return 0;//0表示ListView中数据为空
        } else {
            return list.size() + 1;//这里加1表示要加上优惠卷的界面
        }

    }

    /**
     * getItemViewType和getViewTypeCount能确定ListView中的不同布局的类型和它们的个数
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        int count = list.size();
        if (position < count) { //position<count时说明都是ListView中的item，将它们的位置确定为ITEM1
            Log.e("Basket", "count:" + count + "" + "," + "position:" + position + "");
            return ITEM1;

        } else {
            return ITEM2;
        }

    }

    @Override
    public int getViewTypeCount() {
        return ITEMSUM;//返回布局个数，为2
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        currentType = getItemViewType(position);
        if (currentType == ITEM2) {
            convertView = view_coupon;
            previous_total = (TextView) convertView.findViewById(R.id.previous_total);
            after_total = (TextView) convertView.findViewById(R.id.after_total);
            iv_basket_jump = (ImageView) convertView.findViewById(R.id.iv_basket_jump);
            rl_basket_jump = (RelativeLayout) convertView.findViewById(R.id.rl_basket_jump);
            btn_pay = (Button) convertView.findViewById(R.id.btn_pay);
            rl_basket_jump.setOnClickListener(this);
            btn_pay.setOnClickListener(this);

        } else {
            convertView = View.inflate(context, R.layout.basket_lv_item, null);
            iv_piture = (ImageView) convertView.findViewById(R.id.iv_basket_picture);
            tv_name = (TextView) convertView.findViewById(R.id.tv_basket_name);
            tv_num = (TextView) convertView.findViewById(R.id.tv_basket_num);
            tv_value = (TextView) convertView.findViewById(R.id.tv_basket_value);
            iv_minus = (ImageView) convertView.findViewById(R.id.iv_basket_minus);
            iv_plus = (ImageView) convertView.findViewById(R.id.iv_basket_plus);
            iv_cancle = (ImageView) convertView.findViewById(R.id.iv_item_cancel);
            laundry = list.get(position);

            //ImageLoader初始化
            ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
                    context).threadPoolSize(5).build();
            ImageLoader.getInstance().init(configuration);
            //ImageLoader配置
            DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.mipmap.ic)
                    .displayer(new FadeInBitmapDisplayer(100)).displayer(new RoundedBitmapDisplayer(50)).build();
            //ImageLoader异步加载
            ImageLoader.getInstance().displayImage(laundry.getPicture(), iv_piture, options);

            //每一种衣服的信息
            tv_name.setText(laundry.getPictureName());
            tv_num.setText(laundry.getAmount());
            tv_value.setText(laundry.getValue());
//            itemClick(convertView, position);

            iv_plus.setOnClickListener(this);
            iv_minus.setOnClickListener(this);
            iv_cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    list.remove(position);
                    EventBus.getDefault().post(new CountEvent(2));//发送消息
                }
            });


        }
        return convertView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_basket_jump:
                Intent intent1 = new Intent(context, RegisterActivity.class);
                context.startActivity(intent1);
                break;
            case R.id.btn_pay:
                //点击支付按钮时，将支付金额传递到支付页面
                Intent intent2 = new Intent(context, PayActivity.class);
                intent2.putExtra("amount", after_total.getText().toString());
                context.startActivity(intent2);
                break;
//            //ListView布局的按钮
//            case R.id.iv_basket_minus:
//                clothCount = Integer.parseInt(laundry.getAmount().toString());
//                if (clothCount > 1) {
//                    tv_num.setText((clothCount - 1) + "");
//                }
//                EventBus.getDefault().post(new CountEvent(6));//6表示重新计算
////                BasketAdapter.this.notifyDataSetChanged();//更新数据
//                break;
//            case R.id.iv_basket_plus:
//                clothCount = Integer.parseInt(laundry.getAmount());
//                tv_num.setText((clothCount + 1) + "");
//                Log.e("basket","点击了增加");
//                EventBus.getDefault().post(new CountEvent(6));//6表示重新计算
//                break;
        }
    }
}
